/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author Nguyen Hoang Viet <nguyenhoangvietitt@gmail.com>
 */
public class CalendarDateTime {
     public int WeekinYear(int yy){
        Calendar c1 = new GregorianCalendar();
        c1.set(yy, 0, 1);
        return  c1.getActualMaximum(Calendar.WEEK_OF_YEAR);
    }

    public final static int dom[] = {
        31, 28, 31, 30, /* jan feb mar apr */
        31, 30, 31, 31, /* may jun jul aug */
        30, 31, 30, 31 /* sep oct nov dec */};

    public Calendar WeekToDate(int week,int yy){
        Calendar c = new GregorianCalendar();
        if(week==1){
             c = WeekToDate(WeekinYear(yy-1), yy-1);
             c.add(Calendar.DATE, 7);
             return c;
        }
        else{
            if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) {
                    dom[1] =29;
                }
             for (int month= 0; month < 12; month++) {
                    for (int date= 1; date <= dom[month]; date++) {
                       if(DateToWeek(yy,month,date)==week){
                           //System.out.println(date+"/"+(month+1)+"/"+yy);
                            c.set(yy, month,date);
                            return c;
                       }
                  }
              }
            return c;
        }
    }
    public int DateToWeek(int yy,int mm,int dd){
        int n = 0;
        Calendar c2 = new GregorianCalendar();
        //calender.setTime(a);
        c2.set(yy, mm, dd);
        n = c2.get(Calendar.WEEK_OF_YEAR);
        return n;
    }
}
